package com.todod.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.mapper.DepartmentMapper;
import com.todod.model.Department;
import com.todod.model.User;
import com.todod.service.IDepartmentService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jpf
 * @since 2020-12-14
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

	@Override
	public List<Department> getdepartList() {
		QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_state", "1");
		List<Department> entities = list(queryWrapper);
		// 组装成父子的树形结构
		// 2.1找到所有的一级分类
		List<Department> level1Menus = entities.stream().filter(Department -> Department.getParentId() == 0)
				.map((menu) -> {
					menu.setChildren(getChildrens(menu, entities));
					return menu;
				}).sorted((menu1, menu2) -> {
					return (menu1.getSortNum() == null ? 0 : menu1.getSortNum())
							- (menu2.getSortNum() == null ? 0 : menu2.getSortNum());
				}).collect(Collectors.toList());

		return level1Menus;
	}

	@Override
	public Department getDepartmentById(Long id) {

		return this.getById(id);
	}

	@Override
	public List<Department> geList() {
		QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_state", "1");
		return list(queryWrapper);
	}

	@Override
	public void adddpart(Department department, Long _userId) {
		department.setId(SnowflakeIdGenerator.getId());
		department.setCreateTime(LocalDateTime.now());
		department.setCreateUserId(_userId);
		Long parentId = department.getParentId();
		Department parentDep = getDepartmentById(parentId);

		department.setState("1");
		save(department);
	}

	@Override
	public void editdepart(Department department, Long _userId) {
		department.setModifyTime(LocalDateTime.now());
		department.setModifyUserId(_userId);
		updateById(department);
	}

	/**
	 * 组织架构下的部门列表
	 */
	@Override
	public List<Department> getdepart(Long departId) {
		List<Department> entities = new ArrayList<>();
		Department department = getDepartmentById(departId);
		Department department2 = department;

		List<Department> list = list();
		for (Department departmen : list) {
			entities.add(departmen);
		}
		// 组装成父子的树形结构
		// 2.1找到所有的一级分类
		List<Department> level1Menus = entities.stream().filter(Department -> Department.getId() == department2.getId())
				.map((menu) -> {
					menu.setChildren(getChildrens(menu, entities));
					return menu;
				}).sorted((menu1, menu2) -> {
					return (menu1.getSortNum() == null ? 0 : menu1.getSortNum())
							- (menu2.getSortNum() == null ? 0 : menu2.getSortNum());
				}).collect(Collectors.toList());
		return level1Menus;
	}

	@Override
	public List<Long> getdepatrtid(User byId) {
		Department department = getDepartmentById(byId.getDepartid());
		List<Long> entities = new ArrayList<>();
		List<Department> list = list();
		for (Department department1 : list) {
			entities.add(department1.getId());
		}
		return entities;
	}

	@Override
	public List<Long> getdepatrtid2(Long id) {
		List<Long> entities = new ArrayList<>();
		Department department = getDepartmentById(id);
		if (department.getParentId() == 0) {
			List<Department> list = list();
			for (Department department1 : list) {
				entities.add(department1.getId());
			}
		} else {
			entities.add(department.getId());
		}
		return entities;
	}

	/**
	 * 添加用户角色使用部门列表
	 */
	@Override
	public List<Department> getdepart3(Long id) {
		List<Department> entities = getDownLevenList(id);

		List<Department> level1Menus = buildTree(entities, id);

		return level1Menus;
	}

	@Override
	public List<Department> buildTree(List<Department> entities, Long id) {
		List<Department> level1Menus = entities.stream()
				.filter(Department -> Department.getId().toString().equalsIgnoreCase(String.valueOf(id)))
				.map((menu) -> {
					menu.setChildren(getChildrens(menu, entities));
					return menu;
				}).sorted((menu1, menu2) -> {
					return (menu1.getSortNum() == null ? 0 : menu1.getSortNum())
							- (menu2.getSortNum() == null ? 0 : menu2.getSortNum());
				}).collect(Collectors.toList());
		return level1Menus;
	}

	@Override
	public List<Department> getDownLevenList(Long id) {
		List<Department> entities = new ArrayList<>();
		Department department = getDepartmentById(id);

		QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
		List<Department> list = list(queryWrapper);
		for (Department departmen : list) {
			entities.add(departmen);
		}
		return entities;
	}

	// 获取自己菜单
	private List<Department> getChildrens(Department root, List<Department> all) {
		List<Department> children = all.stream().filter(Department -> {
			return Department.getParentId().equals(root.getId());
		}).map(Department -> {
			// 1.找到子菜单
			Department.setChildren(getChildrens(Department, all));
			return Department;
		}).sorted((menu1, menu2) -> {
			// 2.菜单的顺序
			return (menu1.getSortNum() == null ? 0 : menu1.getSortNum())
					- (menu2.getSortNum() == null ? 0 : menu2.getSortNum());
		}).collect(Collectors.toList());
		return children;
	}

	@Override
	public List<Department> getAllList() {
		QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
		List<Department> list = list(queryWrapper);
		return list;
	}

	/**
	 * 根据父部门编号获取启用子部门列表
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Department> getEnableDepartmentListByParentId(String parentId) {
		QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_parent_id", parentId);
		queryWrapper.eq("sd_state", "1");
		queryWrapper.orderByAsc("sd_sort_num");
		return list(queryWrapper);
	}

	/**
	 * 根据父部门集合获取
	 * 
	 * @Title: getDepartmentListByParentIds
	 * @Description:
	 * @author gsh
	 * @date 2019年7月12日
	 * @param list
	 * @return
	 */
	public List<Department> getEnableDepartmentListByParentIds(List<String> list) {
		QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
		queryWrapper.in("sd_parent_id", list);
		queryWrapper.eq("sd_state", "1");
		queryWrapper.orderByAsc("sd_sort_num");
		return list(queryWrapper);
	}

	@Override
	public List<String> getDownIdList(Department department, Long userId) {

		List<String> list = new ArrayList<>();
		list.add(department.getId().toString());

		List<Department> dList1 = getEnableDepartmentListByParentId(department.getId().toString()); // 大区
		if (dList1 != null && dList1.size() > 0) {

			List<String> ids1 = dList1.stream().map((x) -> x.getId().toString()).collect(Collectors.toList());
			list.addAll(ids1);
			List<Department> dList2 = getEnableDepartmentListByParentIds(ids1);

			list.addAll(dList2.stream().map((x) -> x.getId().toString()).collect(Collectors.toList()));
		}

		return list;
	}

}
